package com.lhb.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhb.dao.TeacherDao;
import com.lhb.pojo.BaseResp;
import com.lhb.pojo.Course;
import com.lhb.pojo.Teacher;
import com.lhb.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.10.10 下午 13:31
 */
@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    TeacherDao teacherDao;

    @Override
    public int del(Integer id) {
        return teacherDao.del(id);
    }

    @Override
    public BaseResp findAll() {
        List<Teacher> all = teacherDao.findAll();
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(0);
        baseResp.setMsg("success");
        baseResp.setData(all);
        return baseResp;
    }

    @Override
    public BaseResp findAllPage(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Course> all = teacherDao.findAllPage();
        PageInfo<Course> of = PageInfo.of(all);
        BaseResp baseResp = new BaseResp();
        baseResp.setCount(of.getTotal());
        baseResp.setData(of.getList());
        baseResp.setCode(0);
        baseResp.setMsg("查询成功");
        return baseResp;
    }

    @Override
    public BaseResp insertTeacher(Teacher teacher) {
        Date date = new Date();
        teacher.setCreateTime(date);
        teacher.setUpdateTime(date);
        int i = teacherDao.insertTeacher(teacher);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(0);
        baseResp.setMsg("添加成功");
        return baseResp;
    }

    @Override
    public BaseResp update(Teacher teacher) {
        int i = teacherDao.update(teacher);
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(1);
        baseResp.setMsg("修改成功");
        return baseResp;
    }
}
